<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="照片" prop="pics">
        <el-input v-model="formData.pics" placeholder="请输入照片" />
      </el-form-item>
      <el-form-item label="总包公司" prop="company">
        <el-input v-model="formData.company" placeholder="请输入总包公司" />
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input v-model="formData.province" placeholder="请输入省" />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input v-model="formData.city" placeholder="请输入市" />
      </el-form-item>
      <el-form-item label="区" prop="area">
        <el-input v-model="formData.area" placeholder="请输入区" />
      </el-form-item>
      <el-form-item label="项目地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入项目地址" />
      </el-form-item>
      <el-form-item label="经度" prop="lon">
        <el-input v-model="formData.lon" placeholder="请输入经度" />
      </el-form-item>
      <el-form-item label="维度" prop="lat">
        <el-input v-model="formData.lat" placeholder="请输入维度" />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="formData.startTime"
          type="date"
          value-format="x"
          placeholder="选择开始时间"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="formData.endTime"
          type="date"
          value-format="x"
          placeholder="选择结束时间"
        />
      </el-form-item>
      <el-form-item label="项目经理id" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入项目经理id" />
      </el-form-item>
      <el-form-item label="财务" prop="financeId">
        <el-input v-model="formData.financeId" placeholder="请输入财务" />
      </el-form-item>
      <el-form-item label="订单总量" prop="orderNumber">
        <el-input v-model="formData.orderNumber" placeholder="请输入订单总量" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as ProjectApi from '@/api/system/project'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  pics: undefined,
  company: undefined,
  province: undefined,
  city: undefined,
  area: undefined,
  address: undefined,
  lon: undefined,
  lat: undefined,
  startTime: undefined,
  endTime: undefined,
  userId: undefined,
  financeId: undefined,
  orderNumber: undefined
})
const formRules = reactive({
  company: [{ required: true, message: '总包公司不能为空', trigger: 'blur' }],
  province: [{ required: true, message: '省不能为空', trigger: 'blur' }],
  city: [{ required: true, message: '市不能为空', trigger: 'blur' }],
  area: [{ required: true, message: '区不能为空', trigger: 'blur' }],
  address: [{ required: true, message: '项目地址不能为空', trigger: 'blur' }],
  lat: [{ required: true, message: '维度不能为空', trigger: 'blur' }],
  userId: [{ required: true, message: '项目经理id不能为空', trigger: 'blur' }],
  financeId: [{ required: true, message: '财务不能为空', trigger: 'blur' }],
  orderNumber: [{ required: true, message: '订单总量不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProjectApi.getProject(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ProjectApi.ProjectVO
    if (formType.value === 'create') {
      await ProjectApi.createProject(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProjectApi.updateProject(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    pics: undefined,
    company: undefined,
    province: undefined,
    city: undefined,
    area: undefined,
    address: undefined,
    lon: undefined,
    lat: undefined,
    startTime: undefined,
    endTime: undefined,
    userId: undefined,
    financeId: undefined,
    orderNumber: undefined
  }
  formRef.value?.resetFields()
}
</script>
